import { queryConsumerCode, queryPretime, queryStoreDetail, queryStoreOptionList, queryMealTime } from '@/services/store';
import { isEmpty } from '@/utils/common';

export default {
  namespace: 'store',
  state: {
    codeDetail: {},//二维码的详情信息,
    preTimeDetail: {},
    storeOptionList: {},
    mealTimeDetail: {}
  },

  effects: {
    *fetchCodeDetail(_: any, { call, put }: IDvaOperation) {
      const response = yield call(queryConsumerCode, localStorage.getItem('CODEID'));
      if (isEmpty(response)) return false;
      yield put({
        type: 'saveCodeDetail',
        payload: response.data || {},
      });
    },

    *fetchPreTime(_: any, { call, put }: IDvaOperation) {
      const response = yield call(queryPretime, 1);
      if (isEmpty(response)) return false;
      yield put({
        type: 'savePreTime',
        payload: response.data || {},
      });
    },
    *fetchMealTime({ payload: { braId } }: any, { call, put }: IDvaOperation) {
      const response = yield call(queryMealTime, braId);
      if (isEmpty(response)) return false;
      yield put({
        type: 'saveMealTime',
        payload: response.data || {},
      });
      if (response.code === 400) {
        yield put({
          type: 'saveMealTime',
          payload: { message: response.message },
        });
      }
    },
    *fetchStoreOptionList(_: any, { call, put }: IDvaOperation) {
      const response = yield call(queryStoreOptionList, 1);
      if (isEmpty(response)) return false;
      yield put({
        type: 'saveStoreOptionList',
        payload: response.data || [],
      });
    }
  },

  reducers: {
    //保存二维码详情
    saveCodeDetail(state: any, action: any) {
      return {
        ...state,
        codeDetail: action.payload
      }
    },
    savePreTime(state: any, action: any) {
      return {
        ...state,
        preTimeDetail: action.payload
      }
    },
    saveStoreOptionList(state: any, action: any) {
      return {
        ...state,
        storeOptionList: action.payload
      }
    },
    saveMealTime(state: any, action: any) {
      return {
        ...state,
        mealTimeDetail: action.payload
      }
    },
  },
}